﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Navigation;

namespace PhoneApp1
{
    public partial class ViewResults : PhoneApplicationPage
    {
        public ViewResults()
        {
            InitializeComponent();
        }

        Poll p;

        private void DisplayMessage(string message, string caption)
        {
            this.Dispatcher.BeginInvoke(
                    delegate
                    {
                        MessageBox.Show(message, caption, MessageBoxButton.OK);
                    });
        }

        protected override void OnNavigatedTo(NavigationEventArgs nea)
        {
            base.OnNavigatedTo(nea);

            String arg = NavigationContext.QueryString["guid"];
            if (arg == "")
            {
                DisplayMessage("Umm... Not sure how you got here... i have nothing to show you...", "Error");
                NavigationService.GoBack();
            }
            Guid g = new Guid(arg);
            foreach (Poll p in ((App)App.Current).fileStorage.df.polls)
            {
                if (p.guid == g)
                {
                    this.p = p;
                }
            }

            questionName.Text = this.p.pollName;
            questionText.Text = this.p.questionText + "\nCreated by: " + this.p.creator.name;
            int votecount = 0;
            foreach (Choice c in this.p.choices)
            {
                votecount = votecount + c.votes;
                
            }
            if (votecount == 0)
                choicesList1.Items.Add("No Votes Have Been Cast!");
            else
            {
                double vp = 0.0; //Vote Percentage
                int count = 0;
                foreach (Choice c in this.p.choices)
                {
                    count++;
                    vp = (c.votes / votecount);
                    choicesList1.Items.Add(count.ToString() + ". " + c.choiceText + ":\t\t" + c.votes.ToString() + " ( " + vp.ToString("p") + " )");
                }
                choicesList1.Items.Add("\nTotal: " + votecount.ToString() + " ( " + votecount.ToString("p") + " )");
            }


        }

        private void V_Back_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.Relative));
        }


    }
}
